package com.example.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.Base.BasePersonActivity;
import com.example.administrator.olddoctor.R;
import com.example.tools.camerautil.BitMap_Camera;
import com.example.tools.camerautil.MyPupupWindos;
import com.example.view.CircleImageView;
import com.example.view.costomGenderSelector.ProvincePick;
import java.util.ArrayList;

import static com.example.Base.BaseContent.path;
import static com.example.Base.BaseContent.DESCRIBE;
import static com.example.Base.BaseContent.EDITPERSONAL_DESCRIBE_REQUSETCODE;
import static com.example.Base.BaseContent.EDITPERSONAL_DESCRIBE_RESULTCODE;
import static com.example.Base.BaseContent.EDITPERSONAL_NIKENAME_REQUSETCODE;
import static com.example.Base.BaseContent.EDITPERSONAL_NIKENAME_RESULTCODE;
import static com.example.Base.BaseContent.HEADER_URLSTR;
import static com.example.Base.BaseContent.NICKNAME;
import static com.example.Base.BaseContent.NULL;
import static com.example.Base.BaseContent.REQUESTCODE_CAMERA;
import static com.example.Base.BaseContent.REQUESTCODE_CROP;
import static com.example.Base.BaseContent.REQUESTCODE_GALLERY;

/**
 * 编辑资料
 */
public class EditPersonalActivity extends BasePersonActivity implements View.OnClickListener{

    private LinearLayout person_edit_image;
    private LinearLayout person_edit_nickname;
    private LinearLayout person_edit_gender;
    private RelativeLayout person_edit_describe;
    private TextView person_edit_nickname_text;
    private TextView person_edit_describe_text;
    private TextView person_edit_gender_text;

    private CircleImageView userImage;
    private MyPupupWindos takePhotoPopWin;
    private BitMap_Camera bitMap_camera;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editpersonal);
        init();
    }

    private void init() {
        setActionBar(getResources().getDrawable(R.mipmap.call_back), null,"编辑资料",null,null,"完成");
        person_edit_image = (LinearLayout) findViewById(R.id.person_edit_image);
        person_edit_nickname = (LinearLayout) findViewById(R.id.person_edit_nickname);
        person_edit_gender = (LinearLayout) findViewById(R.id.person_edit_gender);
        person_edit_describe = (RelativeLayout) findViewById(R.id.person_edit_describe);
        person_edit_nickname_text = (TextView) findViewById(R.id.person_edit_nickname_text);
        person_edit_describe_text = (TextView) findViewById(R.id.person_edit_describe_text);
        person_edit_gender_text = (TextView) findViewById(R.id.person_edit_gender_text);
        userImage = (CircleImageView) findViewById(R.id.userImage);
        person_edit_image.setOnClickListener(this);
        person_edit_nickname.setOnClickListener(this);
        person_edit_gender.setOnClickListener(this);
        person_edit_describe.setOnClickListener(this);
        getActionBarRightText().setOnClickListener(this);
        getActionBarLeft().setOnClickListener(this);
        bitMap_camera = new BitMap_Camera(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.actionbar_right_text:
                showToast("点击了完成");
                break;
            case R.id.actionbar_left:
                finish();
            case R.id.person_edit_image:
                selectImage();
                break;
            case R.id.person_edit_nickname:
                goActivityForResult(this,EditPersonalDetailActivity.class,false,getNickNameBundle(),EDITPERSONAL_NIKENAME_REQUSETCODE);
                break;
            case R.id.person_edit_gender:
                selectGender();
                break;
            case R.id.person_edit_describe:
                goActivityForResult(this,EditPersonalDescribeActivity.class,false,getDescribeBundle(),EDITPERSONAL_DESCRIBE_REQUSETCODE);
                break;
        }
    }

    private void selectImage() {
        //点击更换头像
        takePhotoPopWin = new MyPupupWindos(this , myonClickListener);
        View view = LayoutInflater.from(this).inflate(R.layout.activity_editpersonal,null);
        takePhotoPopWin.showAtLocation(view.findViewById(R.id.layout), Gravity.BOTTOM, 0, 0);
    }



    private void selectGender() {
        ArrayList<String>text = new ArrayList<>();
        text.add("男");
        text.add("女");
        text.add("未知");
        ProvincePick provincePopWin = new ProvincePick.Builder(EditPersonalActivity.this,new ProvincePick.OnProCityPickedListener(){
            @Override
            public void onProCityPickCompleted(String dateDesc) {
                person_edit_gender_text.setText(dateDesc);
            }
        }).textConfirm("完成") //text of confirm button
                .textCancel("取消") //text of cancel button
                .btnTextSize(16) // button text size
                .viewTextSize(25) // pick view text size
                .colorCancel(Color.parseColor("#999999")) //color of cancel button
                .colorConfirm(Color.parseColor("#009900"))//color of confirm button
                .setOneLineList(text) //min year in loop
                .build();
        provincePopWin.showPopWin(EditPersonalActivity.this);
    }

    public Bundle getNickNameBundle(){
        Bundle bundle = new Bundle();
        if(person_edit_nickname_text.getText().toString().equals("未设置")){
            bundle.putString(NICKNAME,NULL);
        } else {
            bundle.putString(NICKNAME,person_edit_nickname_text.getText().toString());
        }
        return bundle;
    }

    public Bundle getDescribeBundle() {
        Bundle bundle = new Bundle();
        if(person_edit_describe_text.getText().toString().equals("未设置")){
            bundle.putString(DESCRIBE,NULL);
        } else {
            bundle.putString(DESCRIBE,person_edit_describe_text.getText().toString());
        }
        return bundle;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (resultCode){
            case EDITPERSONAL_NIKENAME_RESULTCODE:
                person_edit_nickname_text.setText(data.getExtras().getString(NICKNAME));
                break;
            case EDITPERSONAL_DESCRIBE_RESULTCODE:
                String describe = data.getExtras().getString(DESCRIBE);
                person_edit_describe_text.setText(describe);
                break;
        }
        switch (requestCode){
            //打开系统相册请求码
            case REQUESTCODE_GALLERY:
                Uri galleryUri = null;
                try {
                    galleryUri = data.getData();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                if (galleryUri != null) {
                    try {
                        bitMap_camera.gotoCrop(galleryUri);
                    } catch (Exception e) {
                        showToast("请您打开系统相机权限");
                    }
                }
                break;
            //相机回调
            case REQUESTCODE_CAMERA:
                Uri cameraUri = bitMap_camera.getUriFormFile(HEADER_URLSTR);
                if (cameraUri != null) {
                    try {
                        bitMap_camera.gotoCrop(cameraUri);
                    } catch (Exception e) {
                        showToast("请您打开系统相机权限");
                    }
                }
                break;
            //打开前切图片请求码
            case REQUESTCODE_CROP:
                bitmap = null;
                try {
                    bitmap = data.getParcelableExtra("data");
                } catch (Exception e) {
                    showToast("获取图片失败");
                }
                if(bitmap == null){
                    showToast("获取图片失败");
                }

                if (bitmap != null) {
//                    bitmap = bitMap_camera.getRoundCornerBitmap(bitmap, 360);
                    userImage.setImageBitmap(bitmap);
                    BitMap_Camera.saveBitmap(bitmap,path);
                }
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private Bitmap bitmap = null;
    private View.OnClickListener myonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.action_pupupwindos:
                    //打开系统相机
                    try {
                        bitMap_camera.gotoCamera();
                    } catch (Exception e) {
                        showToast("请您打开系统相机权限");
                    }
                    break;
                case R.id.xiance_pupupwindos:
                    //打开系统相册
                    bitMap_camera.gotoGallery();
                    break;
            }

        }
    };


}
